package com.eshop.modules.shop.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.order.domain.EshopExpress;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.order.service.dto.ExpressQueryCriteria;
import com.eshop.modules.order.service.ExpressService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:快递管理" })
@RestController
@RequestMapping({ "api" })
public class ExpressController
{
    private final ExpressService expressService;
    
    public ExpressController(final ExpressService expressService) {
        this.expressService = expressService;
    }
    
    @Log("查询快递")
    @ApiOperation("查询快递")
    @GetMapping({ "/yxExpress" })
    @PreAuthorize("hasAnyRole('admin','YXEXPRESS_ALL','YXEXPRESS_SELECT')")
    public ResponseEntity getYxExpresss(final ExpressQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.expressService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("新增快递")
    @ApiOperation("新增快递")
    @PostMapping({ "/yxExpress" })
    @PreAuthorize("hasAnyRole('admin','YXEXPRESS_ALL','YXEXPRESS_CREATE')")
    public ResponseEntity create(@Validated @RequestBody final EshopExpress resources) {
        return new ResponseEntity(this.expressService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("修改快递")
    @ApiOperation("修改快递")
    @PutMapping({ "/yxExpress" })
    @PreAuthorize("hasAnyRole('admin','YXEXPRESS_ALL','YXEXPRESS_EDIT')")
    public ResponseEntity update(@Validated @RequestBody final EshopExpress resources) {
        this.expressService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除快递")
    @ApiOperation("删除快递")
    @DeleteMapping({ "/yxExpress/{id}" })
    @PreAuthorize("hasAnyRole('admin','YXEXPRESS_ALL','YXEXPRESS_DELETE')")
    public ResponseEntity delete(@PathVariable final Integer id) {
        this.expressService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
